﻿(function(){   //数据表格
$.fn.wtTable=function(option){
    var defaults={
        enableSelect:false,  //是否激活行选中
        multiSelect:false,   //是否激活选中多行
        checkbox:"selectthis",  //选中时连带选中行所在的checkbox,此处设置checkbox类名称
        tdhover:"tdhover",     //定义鼠标移动到行时的背景颜色
        tdselected:"tdselected",  //定义被选中行的背景颜色
        cross:true,                //是否允许交叉行分别颜色
        crossColor:"cross"             //交叉行颜色类
    };
    var options = $.extend({}, defaults,option);
    return this.each(function(){
        if(options.cross){   //交叉行变色
            $(this).find("tr[class!='thead'][class!='tfoot']:odd").addClass(options.crossColor);
        }

        $(this).find("tr[class!='thead'][class!='tfoot']").hover(function(){  //除头尾外.表格焦点高梁
            $(this).addClass(options.tdhover);
        },function(){
            $(this).removeClass(options.tdhover);
        });

        $(this).find("tr[class!='thead'][class!='tfoot']").bind("click",function(){
            if(!options.enableSelect) return;    //如果未配置选中行.return
            if($(this).hasClass(options.tdselected))   //如果当前行已为选中状态,则取消选中
            {
                $(this).removeClass(options.tdselected);
                $(this).find("."+options.checkbox).attr("checked",false);//并且将当前行的选择框去除选择
                return;
            }
            if(!options.multiSelect){  //如果未配置多选中,则将当前行以外的选中状态取消
                $(this).siblings("."+options.tdselected).removeClass(options.tdselected);
                $(this).parent().find("."+options.checkbox).attr("checked",false);
            }
            $(this).addClass(options.tdselected);   //将当前行选中
            $(this).find("."+options.checkbox).attr("checked","true");  //将当前行的选择框选中
        });

        $(this).find("a").bind("click",function(){
            if(event&&event.stopPropagation){//停止冒泡干扰选中
                event.stopPropagation();
            }else{
                event.cancelBubble = true;
            }
        });
    });
}})(jQuery);
/*********************combobox************************************/
(function(){
    var comboboxlist=[];

    function Tcombobox(){}
    Tcombobox.prototype={
        source:null,      //源select
        comboboxhead:null,   //新select头
        comboboxbody:null,   //新select下拉体
        visible:false,       //当前显示状态
        beginfilter:false,   //侦听器开关
        filtering:false,     //侦听状态
        options:null,       //相关操作
        filterPrevalue:undefined,  //相对于本次过滤值的前一次值
        selectvalue:function(){
            return this.source.find("option:selected").val();
        },
        selecttext:function(){
            return this.source.find("option:selected").text();
        },
        items:function(){
            return this.comboboxbody.find(".combo_item");
        },
        selecteditems:function(){
            return this.comboboxbody.find(".combo_item_selected");
        },
        show:function(){    //显示
             if(!this.options.action)return;
             this.comboboxbody.css({position:"absolute",  //下拉框位置
                 top:(this.comboboxhead.position().top+this.comboboxhead.outerHeight())+"px",
                 left:this.comboboxhead.position().left+"px",
                 width:this.comboboxhead.width()+"px"
             });//为其定位位置
             this.comboboxbody.show();
             this.resizeHeight();
             this.visible=true;
        },
        hide:function(){    //隐藏
            this.comboboxbody.hide();
            this.visible=false;
        },
        selectofval:function(val){   //根据选择值选择
            var items=this.items();
            this.selecteditems().removeClass("combo_item_selected");
            for(var i=0;i<items.length;i++){
                if($(items[i]).attr("value")==val)
                {
                    this.select($(items[i]));
                }
            }
        },
        selectoftext:function(text){   //根据选择文本选择
            var items=this.items();
            this.selecteditems().removeClass("combo_item_selected");
            for(var i=0;i<items.length;i++){
                if($(items[i]).text()==text)
                {
                    this.select($(items[i]));
                }
            }
        },
        select:function(item){
            item.addClass("combo_item_selected");
            var val=item.attr("value");
            var changed=val!=this.selectvalue();
            this.source.find("option:selected").attr("selected",false);
            this.source.find("option[value='"+val+"']").attr("selected",true);
            if(changed&&this.source[0].onchange!=undefined){
                this.source.change();
            }
            this.comboboxhead.find(":input").val(this.selecttext());
        },
        refreshSelect:function(){   //核对选择
           // this.selectoftext(this.selecttext());
        },
        resizeHeight:function(){   //重置高度
            var visibleitem =   this.comboboxbody.find(".combo_item:not(:hidden)");
            var showcount=visibleitem.length<this.options.showcount?visibleitem.length:this.options.showcount;
            this.comboboxbody.css("height",(showcount*23)+"px");
            if(showcount<this.options.showcount){
                this.comboboxbody.css("overflow-y","hidden");
            }else{
                this.comboboxbody.css("overflow-y","scroll");
            }
        },
        loadRemoteData:function(){

        }
    };
    function initCombobox(combobox){
        combobox.source.hide();
        combobox.refreshSelect();
        combobox.comboboxhead.unbind("click").bind("click",function(){
            for(var i=0;i<comboboxlist.length;i++)
            {
                if(comboboxlist[i]!=combobox){
                    comboboxlist[i].hide();
                }
            }
            if(combobox.visible){
                combobox.hide();
            }else{
                combobox.show();
            }
            return false;
        });

        combobox.items().unbind("click").bind("click",function(){
            combobox.selectoftext($(this).text());
            combobox.hide();
        });

        combobox.comboboxhead.find(":input").bind("focus",function(){
            if(combobox.options.readonly) return false;
            combobox.beginfilter=true;   //开始挂起监听器
            combobox.filtering=true;     //命令过滤器开始过滤
            setTimeout(function(){filter(combobox)},300);  //这里做一个延迟,主要为了避免和上一级的click发生冲突
        });

        combobox.comboboxhead.find(":input").bind("blur",function(){
            combobox.filtering=false;   //停止过滤
            combobox.beginfilter=false;  //停止监听
            combobox.refreshSelect();
        });

        combobox.comboboxhead.find(":input").bind("keydown",function(e){
            combobox.filtering=true;
            switch(e.keyCode){
                case 13:{
                    combobox.filtering=false;
                    combobox.hide();
                    combobox.refreshSelect();
                }break;
                case 38:combobox.filtering=false;keyselect("up",combobox);break;
                case 40:combobox.filtering=false;keyselect("down",combobox);break;
            }
        });
    }

    function filter(combobox){
        var inputvalue = combobox.comboboxhead.find(":input").val();
        if(combobox.filtering&&inputvalue!=this.filterPrevalue){   //如果有禁用过滤的操作或者值没有改变则不过滤
            if(!combobox.visible){
                combobox.show();
            }
            this.filterPrevalue = inputvalue;
            combobox.items().each(function(){
                if(inputvalue==""
                    ||inputvalue==combobox.selecttext()
                    ||$(this).text().indexOf(inputvalue)!=-1
                    )
                {
                    $(this).show();
                }else{
                    $(this).hide();
                }
            });
            combobox.resizeHeight();
        }
        if(combobox.beginfilter){   //如果过滤动作未结束则一直挂起
            setTimeout(function(){
                filter(combobox);
            },200);
        }
    }

    function keyselect(direction,combobox){    //这里控制键盘选择
        var visibleitem =   combobox.comboboxbody.find(".combo_item:not(:hidden)");
        switch(direction){
            case "up":
                if(combobox.selecteditems().css("display")=="none"){
                    if($(visibleitem[visibleitem.length-1]).length!=0){
                        combobox.selectofval($(visibleitem[visibleitem.length-1]).attr("value"));
                    }
                }else{
                    for(var i=0;i<visibleitem.length;i++){
                        if($(visibleitem[i]).hasClass("combo_item_selected")
                            &&
                            $(visibleitem[i-1]).length!=0
                            ){
                            combobox.selectofval($(visibleitem[i-1]).attr("value"));
                            return;
                        }
                    }
                }
                break;
            case "down":
                if(combobox.selecteditems().css("display")=="none"){
                    if($(visibleitem[0]).length!=0){
                        combobox.selectofval($(visibleitem[0]).attr("value"));
                    }
                }else{
                    for(var i=0;i<visibleitem.length;i++){
                        if($(visibleitem[i]).hasClass("combo_item_selected")
                            &&
                            $(visibleitem[i+1]).length!=0
                            ){
                            combobox.selectofval($(visibleitem[i+1]).attr("value"));
                            return;
                        }
                    }
                }
        }

    }

    $.fn.wtcombobox=function(option){
        var defaults = {showcount:500,action:true,remoteurl:"none",readonly:"false"};
        var options = $.extend({},defaults,option);
        return this.each(function(){
            var tempcombo = new Tcombobox();
            tempcombo.source=$(this);
            tempcombo.options=options;
            tempcombo.comboboxhead = getComboboxHead(this);  //获取下拉框新样式
            tempcombo.comboboxbody = getComboboxBody(this);  //获取下拉框下拉体
            if(!options.action){
                tempcombo.comboboxhead.find(":input").attr("disabled","disabled");
            }
            else{
                tempcombo.comboboxhead.find(":input").attr("readonly",options.readonly);
                if(options.readonly){
                   tempcombo.comboboxhead.find(":input").css("cursor","default");
                }
            }
            tempcombo.comboboxhead.insertAfter($(this));
            tempcombo.comboboxbody.insertAfter(tempcombo.comboboxhead);
            initCombobox(tempcombo);
            comboboxlist.push(tempcombo);
        });
    };


    function getComboboxHead(combobox){   //这里组装combobox显示部分(head)
        combobox = $(combobox);
        var width=combobox.outerWidth();
        width=width||100;
        var comboboxtext = combobox.find("option:selected").text();  //目标选择的文本
        var title = '<span class="combo" style="width:' + (width) + 'px">' +          //为了方便这里直接拼接
            '<div class="comboval" style="width:'+(width-20)+'px"><input type="text" name="'+combobox.attr("id")+'_text" value="'+comboboxtext+'"/></div>'+
            '<span class="arrow"></span>'+
            '</span>';
        title=$(title);
        return title;
    }

    function getComboboxBody(combobox){   //这里组装combobox下拉部分(body)
        combobox = $(combobox);
        var comboboxval = combobox.find("option:selected").val();      //目标选中的值
        var content =$('<div class="combo_item_panel"></div>');   ////为了方便这里直接拼接
        $(combobox).find("option").each(function(){
            var op='<div class="combo_item" value="'+$(this).val()+'">'+$(this).text()+'</div>';  //为定制的DOM循环添加子项
            op=$(op);
            op.hover(function(){
                $(this).addClass("combo_item_hover");
            },function(){
                $(this).removeClass("combo_item_hover");
            });
            op.appendTo(content);
        });
        return content;
    }
    $(document).bind("click",function(){
        for(var i=0;i<comboboxlist.length;i++)
        {
            comboboxlist[i].hide();
        }
    });   //追加绑定document点击事件,用于关闭异常的下拉框);
})(jQuery);

//***********************combobox结束****************************//
//*************************表单验证*******************************//
(function(){
    var rules={
        number: {
            regex: /^[-,+]?\d+(.\d+)?$/,
            message: "* 请输入有效的浮点数"
        },
        integer: {
            regex: /^[-,+]?(([1-9]\d+)|([0-9]))$/,
            message: "* 请输入有效的整数"
        },
        normal: {
            regex: /^[a-zA-Z0-9]+$/,
            message: "* 不能输入特殊字符"
        },
        date: {
            regex: /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/,
            message: "* 无效的日期，格式必需为 YYYY-MM-DD"
        },
        minsize:{
            regex:function(value,rl){
                return value.length>=rl;
            },
            message:"* 最少不能少于@个字符"
        },
        maxsize:{
            regex:function(value,rl){
                return value.length<=rl;
            },
            message:"* 最长不能超过@个字符"
        },
        mindate:{
            regex:function(value,rl){
                return value.length>=rl;
            },
            message:"* 日期不能早于@"
        },
        maxdate:{
            regex:function(value,rl){
                return value.length>=rl;
            },
            message:"* 日期不能晚于@"
        },
        min:{
            regex:function(value,rl){
               if(value==""||!value) return false;
                return parseFloat(value) > parseFloat(rl);
            },
            message:"* 请输入大于@的数"
        },
        max:{
            regex:function(value,rl){
                if(value==""||!value) return false;
                return parseFloat(value) < parseFloat(rl);
            },
            message:"* 请输入小于@的数"
        },
        require:{
            regex:function(value){
                return value != "";
            },
            message:"* 不能为空"
        }
    }

    var Field = function(dom){
        this.dom = dom;
        var that=this;
        this.dom.bind("focus",function(){
            that.hidemessage();
        });
    }
    Field.prototype={
        msgbox:null,
        valdation:function(rules){
            var errormsg="";       //消息记录合集
            var pass=true;
            var type=this.dom.attr("validation").split("|");   //获取本项拥有的匹配规则
            for(var i=0;i<type.length;i++){
                var res=this.resolve(type[i]);
                if(rules[res[0]] == undefined) continue;    //如果此项规则未定义,则默认通过
                var regex = rules[res[0]].regex;
            //    if(res[0]!="require"&& this.dom.val()=="") continue;   //如果输入的值为空,则默认不检测
                if(typeof regex=="object"){                    //如果是正则,则直接验证
                    pass=regex.test(this.dom.val());
                }else if(typeof regex=='function'){
                    pass=regex(this.dom.val(),res[1]);
                }

                if(!pass){
                    errormsg+=rules[res[0]].message.replace("@",res[1])+"<br />";
                }
            }
            this.showmessage(errormsg);
            return errormsg=="";    //如果错误列表不为空,则表示未通过验证
        },
        resolve:function(rule){
            return rule.split("=");
        },
        showmessage:function(message){
            this.hidemessage();
            if(message=="") return;
            var msgbox='<div class="alertmessagebox">'+
                '<div class="hand">'+
                '<div class="line line1"></div>'+
                '<div class="line line2"></div>'+
                '<div class="line line3"></div>'+
                '<div class="line line4"></div>'+
                '<div class="line line5"></div>'+
                '<div class="line line6"></div>'+
                '<div class="line line7"></div>'+
                '<div class="line line8"></div>'+
                '<div class="line line9"></div>'+
                '<div class="line line10"></div>'+
                '</div></div>';
            msgbox=$(msgbox);
            $('<div class="alerttext">'+message+'</div>').appendTo(msgbox);
            msgbox.prependTo("body");
            msgbox.css({left:this.dom.offset().left-20+"px",top:(this.dom.offset().top+this.dom.outerHeight()+8)+"px"});
            this.msgbox = msgbox;
            var that =this;
            msgbox.bind("click",function(){
                that.hidemessage();
            });
            msgbox.show();
        },
        hidemessage:function(){
            if(this.msgbox!=null){
                this.msgbox.remove();
                this.msgbox = null;
            }
        }
    }

    var methods={
        _init:function(options){
            var form=this;
            if(!form.data("wtval")||!form.data("wtval")==null)
            {
                methods._saveOptions(form,options);
            }
            var opts = form.data("wtval");
            form.attr("validation","false");
           // this.bind("submit",methods.submit);  //linkbutton回发不会被出发
            this[0].onsubmit = methods.submit;
            this.find("[activevalidation]").bind("mousedown",function(){
                if($(this).attr("activevalidation")=="true"){
                    form.attr("validation","true");
                }
            });
            methods.addFields.call(form,opts);
            return this;
        },
        _saveOptions:function(form,options){
            $.wtvalidation.defaults.rules= $.extend({},rules,options);   //用于扩展自定义验证
            var userOptions = $.extend({},$.wtvalidation.defaults);
            form.data("wtval",userOptions);
            return userOptions;
        },
        addFields:function(options){
            this.find("[validation]").each(function(){
                var field = new Field($(this));
                options.fields.push(field);
            });
        },
        resolveValidation:function(){},
        normalValidation:function(){},
        customValidation:function(){},
        submit:function(){
            if($(this).attr("validation")=="false") return true;   //如果没有激活验证则直接通过
            var options = $(this).data("wtval");
            var error =0;
            for(var i=0;i<options.fields.length;i++)
            {
                if(!options.fields[i].valdation(options.rules))
                {
                  error++;
                }
            }
            if(Boolean(error))
            {
                try{
                    if (progress.box) {
                        progress.hide();
                    }
                }catch(e){}
            }
            $(this).attr("validation","false");
            return !Boolean(error);
        }
    }

    $.fn.wtvalidation=function(){
        if(!$(this)[0]) return;//如果表单不存在,则跳出

        if(!$(this).is("form")){   //只允许验证表单内的控件
            alert("失败,验证插件只允许在form内进行验证");
            return;
        }
        var form = $(this);
        return methods._init.apply(form,arguments);
    }

    $.wtvalidation={
        defaults:{
            rules:null,
            fields:[]
        }
    }
})(jQuery);
//*****************************表单验证在此结束**************************//
//*****************************分页插件**************************//
$(function() {
    $(".pager .exportpanel").hover(function() {
        if ($(this).find(".export").length > 0) {
            $(this).find(".exportmenu").show();
        }
    }, function() {
        $(this).find(".exportmenu").hide();
    });
})